# -*- coding: utf-8 -*-
import json
import pymssql
def readjson():
    f = open("shengyiwu.json", encoding = "utf-8")
    file = json.load(f)
    return [file["flower"],file["feather"],file["sand"],file["cup"],file["head"]]
class writedata:
    nametransfer = {'noblesseOblige': '昔日宗室之仪', 'paleFlame': '苍白之火','tenacityOfTheMillelith': '千岩牢固', 'heartOfDepth': '沉沦之心', 'blizzardStrayer': '冰封迷途的勇士', 'crimsonWitch': '炽烈的炎之魔女', 'lavaWalker': '渡过烈火的贤人', 'viridescentVenerer': '翠绿之影', 'maidenBeloved': '被怜爱的少女', 'bloodstainedChivalry': '染血的骑士道', 'wandererTroupe': '流浪大地的乐团', 'gladiatorFinale': '角斗士的终幕礼','thunderSmoother': '平息鸣雷的尊者'}
    attributetrans = {'lifePercentage' :'生命值百分比', 'attackPercentage': '攻击力百分比', 'defendPercentage': '防御力百分比', 'lifeStatic':'生命值','critical':'暴击率','criticalDamage': '暴击伤害', 'attackStatic': '攻击力', 'elementalMastery': '元素精通', 'recharge': '元素充能效率', 'defendStatic': '防御力', 'physicalBonus': '物理伤害加成', 'cureEffect': '治疗加成', 'rockBonus': '岩元素伤害加成', 'windBonus': '风元素伤害加成', 'iceBonus': '冰元素伤害加成', 'waterBonus' : '水元素伤害加成', 'fireBonus' : '火元素伤害加成', 'thunderBonus': '雷元素伤害加成'}
    idnum = 10000
    def __init__(self,relics, transfer):
        self.relics = relics
        self.conn = pymssql.connect(host = 'localhost', server = 'DESKTOP-7CI5VVB', port = '1074', user = 'boqi', password = '123456', database = 'genshi')
        self.cursor = self.conn.cursor()
        if transfer:
            for pos in self.relics:
                for ob in pos:
                    ob["setName"] = self.nametransfer[ob["setName"]]
                    ob["mainTag"]["name"] = self.attributetrans[ob["mainTag"]["name"]]
                    for tags in ob["normalTags"]:
                        tags["name"] = self.attributetrans[tags["name"]]
        self.insertdata() #插入数据
        self.cursor.close()
        self.conn.close()
    def insertdata(self):
        for pos in self.relics:
            for ob in pos:
                if len(ob["normalTags"]) == 3:
                    sql = "insert into Relics(id, position, setName, detailName, mainTagname, mainTagvalue, Tagname_1, value_1, Tagname_2, value_2, Tagname_3, value_3, grade) values('%d','%s','%s','%s','%s','%f','%s','%f','%s','%f','%s','%f','%d');commit" %(self.idnum, ob["position"], ob["setName"], ob["detailName"], ob["mainTag"]["name"], ob["mainTag"]["value"], ob["normalTags"][0]["name"], ob["normalTags"][0]["value"], ob["normalTags"][1]["name"], ob["normalTags"][1]["value"], ob["normalTags"][2]["name"], ob["normalTags"][2]["value"], ob["level"])
                elif len(ob["normalTags"]) == 4:
                    sql = "insert into Relics(id, position, setName, detailName, mainTagname, mainTagvalue, Tagname_1, value_1, Tagname_2, value_2, Tagname_3, value_3, Tagname_4, value_4, grade) values('%d','%s','%s','%s','%s','%f','%s','%f','%s','%f','%s','%f','%s','%f','%d');commit" %(self.idnum, ob["position"], ob["setName"], ob["detailName"], ob["mainTag"]["name"], ob["mainTag"]["value"], ob["normalTags"][0]["name"], ob["normalTags"][0]["value"], ob["normalTags"][1]["name"], ob["normalTags"][1]["value"], ob["normalTags"][2]["name"], ob["normalTags"][2]["value"], ob["normalTags"][3]["name"], ob["normalTags"][3]["value"], ob["level"])
                else:
                    raise Exception("error")
                try:
                    self.cursor.execute(sql)
                except:
                    self.conn.rollback()
                self.idnum += 1
        

w = writedata(readjson(), True) #参数1为要导入的数据，参数2为是否做名称转换
    